﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace App_CommonLIB.Util
{
    public class ConvertUtil
    {
        public static char ThousandSeparator = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator);
        public static char DecimalSeparator = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);

        public static DateTime DefaultDateTime = new DateTime(1990, 1, 1, 0, 0, 0);

        private static string strDisplayedFormatDatetime = "dd/MM/yyyy";
        private static string strStoredFormatDatetime = "yyyyMMdd";

        public static int DefaultInt = 0;

        public static string ToNumericString(long? number)
        {
            return (number != null) ? ((long)number).ToString("N0") : "0";
        }

        public static string ToPlainString(long? number)
        {
            return (number != null) ? ((long)number).ToString() : "0";
        }

        public static string ToDisplayedString(DateTime? time)
        {
            DateTime timeNotNull = (time != null) ? ((DateTime)time) : DefaultDateTime;
            return timeNotNull.ToString(strDisplayedFormatDatetime);
        }

        public static string ToStoredString(DateTime? time)
        {
            DateTime timeNotNull = (time != null) ? ((DateTime)time) : DefaultDateTime;
            return timeNotNull.ToString(strStoredFormatDatetime);
        }

        public static int ToInt(string strNumber)
        {
            if (strNumber == null) { return 0; }
            strNumber = strNumber.Replace(ThousandSeparator.ToString(), "");
            if (strNumber == "") { return 0; }
            return int.Parse(strNumber);
        }

        public static long ToLong(string strNumber)
        {
            if (strNumber == null) { return 0; }
            strNumber = strNumber.Replace(ThousandSeparator.ToString(), "");
            if (strNumber == "") { return 0; }
            return long.Parse(strNumber);
        }

        public static decimal ToDecimal(string strNumber)
        {
            if (strNumber == null) { return 0; }
            strNumber = strNumber.Replace(ThousandSeparator.ToString(), "");
            if (strNumber == "") { return 0; }
            return decimal.Parse(strNumber);
        }

        public static DateTime ToDefault(DateTime? value)
        {
            return (value == null) ? DefaultDateTime : (DateTime)value;
        }

        public static long ToDefault(long? value)
        {
            return (value == null) ? DefaultInt : (long)value;
        }
        
        public static int ToDefault(int? value)
        {
            return (value == null) ? DefaultInt : (int)value;
        }
    }
}